package com.itheima.tanhua.web;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.itheima.tanhua.pojo.db.Answer;
import com.itheima.tanhua.pojo.db.Option;
import com.itheima.tanhua.pojo.db.SoulQuestion;
import com.itheima.tanhua.service.TestSoulService;
import com.itheima.tanhua.vo.mongo.TestSoulVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("testSoul")
public class TestSoulController {

    @Autowired
    TestSoulService testSoulService;
    /**
     * 测试列表
     * @return
     */
    @GetMapping
    public List<TestSoulVo> testSoulList(){
        List<TestSoulVo>list=testSoulService.getAllTestSoulVo();



       /* ArrayList questions = new ArrayList();
        ArrayList options = new ArrayList();
        options.add(new Option("A","A早晨"));
        SoulQuestion soulQuestion =new SoulQuestion(
                "1",
                "1、你何时感觉最好？",
                options
        );
        questions.add(soulQuestion);
        TestSoulVo testSoulVo = new TestSoulVo(
                "1",
                "问卷名称",
                "https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/tanhua/avatar_7.jpg",
                "初级测试",
                3,
                questions,
                0,
                null
                );
        List<TestSoulVo> list=new ArrayList<>();
        list.add(testSoulVo);*/
        return list;
    }

    /**
     * 报告id
     * @param id
     * @return
     */
    @GetMapping("report/{id}")//1
    public ResponseEntity report(@PathVariable Long id){
        //根据报告id获得对应的对象返回
        return ResponseEntity.ok("成功");
    }

    /**
     * 提交答案
     * @param
     * @return
     */
    @PostMapping
    public String testSoul(@RequestBody String answers){
        Map<String,List<Answer>> map = JSON.parseObject(answers, Map.class);
        List<Answer> answers1 = map.get("answers");
        List<String> optionId = CollUtil.getFieldValues(answers1, "optionId",String.class);
        int total=testSoulService.getTotalScore(optionId);
        //todo修改下一关的锁和修改对应的报告id
       //报告id:根据分数获得
//        String reportId=totalScore.getReportId(total);


        return total+"";//报告id
    }

    public static void main(String[] args) {
        String answers="{\"answers\":[{\"questionId\":\"1\",\"optionId\":\"1\"},{\"questionId\":\"2\",\"optionId\":\"4\"},{\"questionId\":\"3\",\"optionId\":\"9\"},{\"questionId\":\"4\",\"optionId\":\"15\"},{\"questionId\":\"5\",\"optionId\":\"19\"},{\"questionId\":\"6\",\"optionId\":\"24\"},{\"questionId\":\"7\",\"optionId\":\"26\"},{\"questionId\":\"8\",\"optionId\":\"29\"},{\"questionId\":\"9\",\"optionId\":\"36\"},{\"questionId\":\"10\",\"optionId\":\"41\"},{\"questionId\":\"10\",\"optionId\":\"41\"},{\"questionId\":\"10\",\"optionId\":\"41\"},{\"questionId\":\"10\",\"optionId\":\"41\"},{\"questionId\":\"10\",\"optionId\":\"41\"},{\"questionId\":\"10\",\"optionId\":\"40\"},{\"questionId\":\"10\",\"optionId\":\"40\"},{\"questionId\":\"10\",\"optionId\":\"40\"}]}\n";
        Map<String,List<Answer>> map = JSON.parseObject(answers, Map.class);
        List<Answer> answers1 = map.get("answers");
        List<String> optionId = CollUtil.getFieldValues(answers1, "optionId",String.class);
        System.out.println(answers1);
        /*name="[{\"questionId\":\"1\",\"optionId\":\"1\"},{\"questionId\":\"2\",\"optionId\":\"4\"},{\"questionId\":\"3\",\"optionId\":\"9\"},{\"questionId\":\"4\",\"optionId\":\"15\"},{\"questionId\":\"5\",\"optionId\":\"19\"},{\"questionId\":\"6\",\"optionId\":\"24\"},{\"questionId\":\"7\",\"optionId\":\"26\"},{\"questionId\":\"8\",\"optionId\":\"29\"},{\"questionId\":\"9\",\"optionId\":\"36\"},{\"questionId\":\"10\",\"optionId\":\"41\"},{\"questionId\":\"10\",\"optionId\":\"41\"},{\"questionId\":\"10\",\"optionId\":\"41\"},{\"questionId\":\"10\",\"optionId\":\"41\"},{\"questionId\":\"10\",\"optionId\":\"41\"},{\"questionId\":\"10\",\"optionId\":\"40\"},{\"questionId\":\"10\",\"optionId\":\"40\"},{\"questionId\":\"10\",\"optionId\":\"40\"}]";
        List<Answer> answerList = JSON.parseArray(name, Answer.class);
        System.out.println(answerList);*/
    }



}
